package com.apes.scm.account.account.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apes.framework.api.DomainService;
import com.apes.framework.api.SpringManager;
import com.apes.framework.plugin.esb.api.SimpleRequest;


import com.apes.framework.query.QueryService;
import com.apestech.framework.json.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by lishikun on 2020/11/6 13:43
 */
@Service("stockInventoryBatchService")
public class StockInventoryService  extends DomainService {

    @Autowired
    QueryService queryService;

    public void rptStockInventory(SimpleRequest request) throws Exception {
        Date currentTime = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Date date = addOrReduceTime(currentTime, -1);
        String sDate = sdf.format(date);
        Date datenew = sdf.parse(sDate);


        EntityManager entityManager = SpringManager.getBean(EntityManager.class);
        Query query = entityManager.createNativeQuery("{call SUM_STOCK_INVENTORY(?)}")
                .setParameter(1, datenew);
        int count = query.executeUpdate();
        System.out.println("----------------:" + count);
    }


    /**
     * 获取加减后的时间
     *
     * @param date
     * @param day
     * @return
     * @throws Exception
     */
    private Date addOrReduceTime(Date date, int day) throws Exception {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, day);//-1今天的时间加一天

        Date dateNeed = calendar.getTime();

        return dateNeed;
    }

    private com.apestech.framework.json.JSONObject stockInventoryMonth(SimpleRequest request) throws Exception {

        JSONObject jo = request.getJO();
        JSONArray condition = jo.getJSONArray("condition");
        if (condition != null) {
            for (int i = 0; i < condition.size(); i++) {
                if ("GTE".equals(condition.getJSONObject(i).getString("operator"))) {
                    StringBuilder sb = new StringBuilder(condition.getJSONObject(i).getString("value"));
                    condition.getJSONObject(i).put("value",sb.replace(8, 10, "01").toString());
                }
                if ("LTE".equals(condition.getJSONObject(i).getString("operator"))) {
                    StringBuilder sb = new StringBuilder(condition.getJSONObject(i).getString("value"));
                    condition.getJSONObject(i).put("value",sb.replace(8, 10, "01").toString());
                }
            }
            jo.put("condition", condition);
        }

        return JSON.toJSON(jo.toJSONString());
    }
    private com.apestech.framework.json.JSONObject stockInventoryDay(SimpleRequest request) throws Exception {


        JSONObject jo = request.getJO();
        return JSON.toJSON(jo.toJSONString());


    }

}
